import { get } from './base';

export function procressSongs(songs) {
  if (!songs.length) {
    return Promise.resolve(songs);
  }
  return get('/api/getSongsUrl', {
    mid: songs.map((song) => {
      return song.mid;
    })
  }).then(result => {
    const map = result.map;
    console.log(result, 'songs');
    return songs.map(song => {
      song.url = map[song.mid];
      return song;
    }).filter((song) => {
      return song.url && song.url.indexOf('vkey') > -1;
    });
  });
}

// 获取歌词
const lyricMap = {}; // 临时歌曲存储
export function getLyric(song) {
  if (song.lyric) {
    return Promise.resolve(song.lyric);
  }
  const mid = song.mid;
  const lyric = lyricMap[mid];
  if (lyric) {
    return Promise.resolve(lyric);
  }
  return get('/api/getLyric', {
    mid
  }).then((result) => {
    const lyric = result || '[00:00:00]该歌曲暂时无法获取歌词';
    lyricMap[mid] = lyric;
    return lyric.lyric;
  });
}
